
clear all
do "...\First.do"

********************************************************************************

clear
use "$work\full_sample.dta"

keep if inrange(age,40,70)
sort pnr year
tab year
drop if year>2019

** GRAPH
rename udd_level udd_kode
destring udd_kode, replace
tab udd_kode

g udd_group=1 if inlist(udd_kode,10) 
replace udd_group=2 if inlist(udd_kode,15,20,25)
replace udd_group=3 if udd_kode==35 | udd_kode==30
replace udd_group=4 if udd_kode==40
replace udd_group=5 if udd_kode==50
replace udd_group=6 if udd_kode>50
replace udd_group=. if udd_kode==.

lab define udd_group2 1 "Primary education" 2 "High School" 3 "Vocational Education" 4 "Short tertiary education" 5 "College" 6 "University"
lab values udd_group udd_group2


foreach var of varlist death death_CVC death_cancer death_cancer_lung death_diabetes death_COPD male yob dk {
    
	bys pnr: egen max_`var'=max(`var')
	
}



replace services=. if GP_visit==0

foreach var of varlist GP_visit N_visits services total_cost_GP statins ACE COPD_med ACSC_COPD metformin diabetes_control first_lung yder_ses_max married {

    
	bys pnr: egen mean_`var'=mean(`var')
	
}


save "$work\temp_sumstat.dta", replace

clear
use "$work\temp_sumstat.dta"


keep if inrange(age,40,70)
drop if year>2019
drop if min_year==2017
tab timing

keep pnr low_ses mean_* max_* udd_group
drop mean_age mean_dk mean_grades mean_male

duplicates drop


merge 1:1 pnr using "$work\analysis_sample_pnr.dta"

g sample=(_merge==3)
g all=1
drop _merge

********************************************************************************
tab udd_group, g(udd_)

rename max_* *
rename mean_* *

label var male "Male"
label var dk "Ethnical Danish"
label var married "Married"

rename COPD_med COPD_meds
rename services mean_services	
rename N_visits2 N_visits
rename GP_visit GP_contact 
	
label var statins "Statins"
label var metformin "Metformin"
label var COPD_meds "COPD medicine"
label var diabetes_control "Diabetes control"
label var ACE "ACE"
label var first_lung "Lung scan" 

label var ACSC_COPD "COPD hospitalization"
label var mean_services "Number of services per visit"
label var N_visits "Number of visits"
lab var GP_contact "GP contact"

label var death "Death"
lab var death_CVC "CVC death"
lab var death_cancer "Cancer death" 
lab var death_cancer_lung "Lung cancer death"

lab var total_cost_GP "Total reimbursement GP"

lab var udd_1 "Primary education"
lab var udd_2 "High school"
lab var udd_3 "Vocational education"
lab var udd_4 "Associate degree"
lab var udd_5 "Undergraduate degree"
lab var udd_6 "Postgraduate degree"


replace total_cost_GP=total_cost_GP/7.5
set matsize 10000

preserve
capture program drop mystat
program mystat
	syntax varlist, [append replace]
	cap file close myfile
	file open myfile using "$table\TableD1.tex", `append' `replace' write
	local l`varlist': variable label `varlist'
	qui: sum `varlist'
	local a: disp %8.3f r(mean)
	local b: disp %8.3f r(sd)


	qui: ttest `varlist', by(sample)
	local a2: disp %8.3f r(mu_2)
	local b2: disp %8.3f r(sd_2)
	
	qui: sum `varlist' if sample==1 & low_ses==0
	local a3: disp %8.3f r(mean)
	local b3: disp %8.3f r(sd)

	qui: sum `varlist' if sample==1 & low_ses==1
	local a4: disp %8.3f r(mean)
	local b4: disp %8.3f r(sd)

	
	local lab: variable label `varlist'	
	file write myfile "`l`varlist'' & `a' & `a2' & `a3' & `a4' \\ " _n
	file close myfile
end

	file open myfile using "$table\TableD1.tex", replace write
	file write myfile " & (1) & (2) & (3) & (4)  \\" _n
	file write myfile " & Population & Analysis sample & High-SES & Low-SES  \\" _n
	file write myfile " \toprule" _n
	file write myfile " & & & &  \\" _n

	
foreach v of varlist udd_1 udd_2 udd_3 udd_4 udd_5 udd_6 GP_contact N_visits mean_services total_cost_GP death death_CVC death_cancer death_cancer_lung statins ACE COPD_meds ACSC_COPD metformin diabetes_control first_lung {


mystat `v', append


}



file open myfile using "$table\TableD1.tex", append write
sum male if all==1
local i: disp %11.0gc r(N)
sum male if sample==1
local ii: disp %11.0gc r(N)
sum male if sample==1 & low_ses==0
local i2: disp %11.0gc r(N)
sum male if sample==1 & low_ses==1
local ii2: disp %11.0gc r(N)
file write myfile "Number of observations & `i' & `ii' & `i2' & `ii2'  \\" 
file close myfile


restore
********************************************************************************
********************************************************************************


preserve

capture program drop mystat
program mystat
	syntax varlist, [append replace]
	cap file close myfile
	file open myfile using "$table\Table1_PanelA.tex", `append' `replace' write
	local l`varlist': variable label `varlist'
	qui: sum `varlist'
	local a: disp %8.3f r(mean)
	local b: disp %8.3f r(sd)


	qui: ttest `varlist', by(sample)
	local a2: disp %8.3f r(mu_2)
	local b2: disp %8.3f r(sd_2)
	
	qui: sum `varlist' if sample==1 & low_ses==0
	local a3: disp %8.3f r(mean)
	local b3: disp %8.3f r(sd)

	qui: sum `varlist' if sample==1 & low_ses==1
	local a4: disp %8.3f r(mean)
	local b4: disp %8.3f r(sd)

	
	local lab: variable label `varlist'	
	file write myfile "`l`varlist'' & `a' & `a2' & `a3' & `a4' \\ " _n
	file close myfile
end

	file open myfile using "$table\Table1_PanelA.tex", replace write
	file write myfile " & (1) & (2) & (3) & (4)  \\" _n
	file write myfile " & Population & Analysis sample & High-SES & Low-SES  \\" _n
	file write myfile " \toprule" _n
	file write myfile " & & & &  \\" _n

	
foreach v of varlist male yob dk married low_ses yder_ses_max {


mystat `v', append


}



file open myfile using "$table\Table1_PanelA.tex", append write
sum male if all==1
local i: disp %11.0gc r(N)
sum male if sample==1
local ii: disp %11.0gc r(N)
sum male if sample==1 & low_ses==0
local i2: disp %11.0gc r(N)
sum male if sample==1 & low_ses==1
local ii2: disp %11.0gc r(N)
file write myfile "Number of observations & `i' & `ii' & `i2' & `ii2'  \\" 
file close myfile

restore